/**
 *
 * @author JOKE
 * Deck
 */
package NinetyNine;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

public class Deck implements Serializable{
    private ArrayList<Card> CardList = null;
    
    public Deck(boolean i){
        this.CardList = new ArrayList<Card>(52);
        for(int suit=1;suit<=4;suit++) {
            for(int value=1;value<=13;value++) {
                this.CardList.add(new Card(value, suit));
            }
        }
     
    }
    
    public Deck(){
        this.CardList = new ArrayList<Card>();
    }

    public boolean add(Card card)
    {
        if(card.isValid()) {
            this.CardList.add(card);
            return true;
        }
        return false;
    }
    public void shuffle(int times)
    {
        int i,index,j,temp,rand;
        for (temp = 0; temp < times; temp++) {
            i = 1;
            for (index = (int) Math.ceil(this.CardList.size() / 2);
                i < this.CardList.size(); i++) {
                this.CardList.add(i, this.CardList.remove(index));
                i = i + 2;
            }

            rand = (int)( Math.random() * CardList.size());
            for (j = 0; j < rand; j++) {
                this.CardList.add(0, this.CardList.remove(this.CardList.size()-1));
            }
        }
        return;
    }

    public Card draw() {
        return this.CardList.remove(0);
    }

    public boolean remove(Card card) {
        int i=0;
        while(i < this.CardList.size()) {
            if(this.CardList.get(i).equalTo(card)) {
                this.CardList.remove(i);
                return true;
            }
            i++;
        }
        return false;
    }

    public int size() {
        return this.CardList.size();
    }

    public boolean stack(Deck deck) {
        return this.CardList.addAll(deck.getAList());
    }

    public void empty() {
        this.CardList.clear();
    }

    private ArrayList<Card> getAList() {
        return this.CardList;
    }

    @Override
    public String toString() {
        String retVal="";
        for(int temp=0;temp<this.CardList.size();temp++) {
            retVal+=this.CardList.get(temp)+"\n";
        }
        return retVal;
    }

}
